package com.dartfar.base.action;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.model.Function;
import com.dartfar.base.model.Person;
import com.dartfar.base.model.Role;
import com.dartfar.base.model.T_User;
import com.dartfar.base.service.AuthorityService;
import com.dartfar.base.service.FunctionService;
import com.dartfar.base.service.PersonService;
import com.dartfar.base.service.RoleService;
import com.dartfar.base.service.UserService;
import com.dartfar.base.vo.RoleVo;
import com.dartfar.dictionary.model.Dictcatalog;
import com.dartfar.dictionary.model.Dictionary;
import com.dartfar.dictionary.service.DictcatalogService;
import com.dartfar.dictionary.service.DictionaryService;
import com.dartfar.menu.model.Menu;
import com.dartfar.menu.service.MenuService;
import com.dartfar.organize.model.Department;
import com.dartfar.organize.service.DepartmentService;
import com.dartfar.permission.model.Permission;
import com.dartfar.permission.service.PermissionService;
import com.dartfar.util.W7;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 始初化Action对象
 * @author 刘毅
 */
@Component("initSystemAction")
@Scope("prototype")
public class InitSystemAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	/**
	 * Service对象注入
	 */
	@Resource
	private DepartmentService departService;
	@Resource
	private RoleService roleService;
	@Resource
	private UserService userService;
	@Resource
	private MenuService menuService;
	@Resource
	private FunctionService functionService;
	@Resource
	private AuthorityService authorityService;
	@Resource
	private PermissionService permissionService;
	@Resource
	private PersonService personService;
	@Resource
	private DictcatalogService dictcatalogService;
	@Resource
	private DictionaryService dictionaryService;
	
	
	/**
	 * 生成表结构
	 */
	public String create(){
		Configuration cfg = new AnnotationConfiguration().configure();		
		SchemaExport export = new SchemaExport(cfg);		
		export.create(true, true);
		this.addFieldError("message", "初始化表结构完成!");
		return SUCCESS;
	}
	
	/**
	 * 更新表结构
	 */
	public String update(){
		Configuration cfg = new AnnotationConfiguration().configure();		
		SchemaUpdate export = new SchemaUpdate(cfg);		
		export.execute(true, true);
		this.addFieldError("message", "更新表结构完成!");
		return SUCCESS;
	}
	
	/**
	 * 生成系统数据
	 */
	public String systemData(){


		/**
		 * 部门初始化
		 */
		Department depart1 = new Department();
		depart1.setName("总公司");
		depart1.setTreeSign("\\总公司");
		depart1.setLeaf(false);
		departService.save(depart1);
		
		Department depart2 = new Department();
		depart2.setName("子公司");
		depart2.setTreeSign("\\总公司\\子公司");
		depart2.setParentId(1);
		depart2.setLeaf(true);
		departService.save(depart2);
				
		/**
		 * 功能初始化
		 */
		//1
		Function function = new Function();
		function.setName("无");
		function.setFuncode("null");
		function.setIconCls("icon-app");
		function.setCode("null");
		function.setDes("无");
		functionService.save(function);
		//2
		Function function11 = new Function();
		function11.setName("打印");
		function11.setFuncode("print");
		function11.setCode("print");
		function11.setDes("打印");
		function11.setIconCls("icon-print");
		functionService.save(function11);
		//3
		Function function8 = new Function();
		function8.setName("浏览");
		function8.setFuncode("view");
		function8.setCode("view");
		function8.setDes("浏览自己");
		function8.setIconCls("icon-view");
		functionService.save(function8);
		//4
		Function function9 = new Function();
		function9.setName("浏览");
		function9.setFuncode("viewDepart");
		function9.setCode("view");
		function9.setDes("浏览本部门");
		function9.setIconCls("icon-view");
		functionService.save(function9);
		//5
		Function function10 = new Function();
		function10.setName("浏览");
		function10.setFuncode("viewAll");
		function10.setCode("view");
		function10.setDes("浏览全部");
		function10.setIconCls("icon-view");
		functionService.save(function10);
		//6
		Function function1 = new Function();
		function1.setName("增加");
		function1.setFuncode("add");
		function1.setCode("add");
		function1.setDes("增加");
		function1.setIconCls("icon-add");
		functionService.save(function1);
		//7
		Function function2 = new Function();
		function2.setName("编辑");
		function2.setFuncode("edit");
		function2.setCode("edit");
		function2.setDes("编辑自己");
		function2.setIconCls("icon-edit");
		functionService.save(function2);
		//8
		Function function3 = new Function();
		function3.setName("编辑");
		function3.setFuncode("editDepart");
		function3.setCode("edit");
		function3.setDes("编辑本部门");
		function3.setIconCls("icon-edit");
		functionService.save(function3);
		//9
		Function function4 = new Function();
		function4.setName("编辑");
		function4.setFuncode("editAll");
		function4.setCode("edit");
		function4.setDes("编辑全部");
		function4.setIconCls("icon-edit");
		functionService.save(function4);
		//10
		Function function5 = new Function();
		function5.setName("删除");
		function5.setFuncode("del");
		function5.setCode("del");
		function5.setDes("删除自己");
		function5.setIconCls("icon-remove");
		functionService.save(function5);
		//11
		Function function6 = new Function();
		function6.setName("删除");
		function6.setFuncode("delDepart");
		function6.setCode("del");
		function6.setDes("删除本部门");
		function6.setIconCls("icon-remove");
		functionService.save(function6);
		//12
		Function function7 = new Function();
		function7.setName("删除");
		function7.setFuncode("delAll");
		function7.setCode("del");
		function7.setDes("删除全部");
		function7.setIconCls("icon-remove");
		functionService.save(function7);
		//13
		Function function14 = new Function();
		function14.setName("保存");
		function14.setFuncode("save");
		function14.setCode("save");
		function14.setDes("保存");
		function14.setIconCls("icon-save");
		functionService.save(function14);
		//14
		Function function13 = new Function();
		function13.setName("查询");
		function13.setFuncode("search");
		function13.setCode("search");
		function13.setDes("查询");
		function13.setIconCls("icon-search");
		functionService.save(function13);
		//15
		Function function12 = new Function();
		function12.setName("导出");
		function12.setFuncode("export");
		function12.setCode("export");
		function12.setDes("导出");
		function12.setIconCls("icon-excel");
		functionService.save(function12);

		
		/**
		 * 菜单初始化
		 */
		//1
		Menu mainMenu1 = new Menu();
		mainMenu1.setName("系统管理");
		mainMenu1.setMenuOrder(98);
		mainMenu1.setLeaf(false);
		menuService.add(mainMenu1);
		//2
		Menu menu1 = new Menu();
		menu1.setName("部门管理");
		menu1.setTarget("depart!show");
		menu1.setMenuOrder(1);
		menu1.setCode("Depart");
		menu1.setMainMenu(mainMenu1);
		menu1.setIconCls("icon-menu-depart");
		menu1.setLeaf(true);
		menuService.add(menu1);
		//3
		Menu menu2 = new Menu();
		menu2.setName("角色管理");
		menu2.setTarget("role!show");
		menu2.setMenuOrder(2);
		menu2.setCode("Role");
		menu2.setMainMenu(mainMenu1);
		menu2.setIconCls("icon-menu-role");
		menu2.setLeaf(true);
		menuService.add(menu2);
		//4
		Menu menu3 = new Menu();
		menu3.setName("菜单管理");
		menu3.setTarget("menu!show");
		menu3.setMainMenu(mainMenu1);
		menu3.setMenuOrder(3);
		menu3.setCode("Menu");
		menu3.setIconCls("icon-menu-menu");
		menu3.setLeaf(true);
		menuService.add(menu3);
		//5
		Menu menu4 = new Menu();
		menu4.setName("用户管理");
		menu4.setTarget("user!show");
		menu4.setCode("User");
		menu4.setMainMenu(mainMenu1);
		menu4.setMenuOrder(4);
		menu4.setIconCls("icon-menu-user");
		menu4.setLeaf(true);
		menuService.add(menu4);
		//6
		Menu menu5 = new Menu();
		menu5.setName("权限管理");
		menu5.setTarget("permission!show");
		menu5.setCode("Permission");
		menu5.setMainMenu(mainMenu1);
		menu5.setMenuOrder(5);
		menu5.setIconCls("icon-menu-authority");
		menu5.setLeaf(true);
		menuService.add(menu5);
		//7
		Menu menu8 = new Menu();
		menu8.setName("字典管理");
		menu8.setTarget("dictcatalog!show");
		menu8.setCode("Dictcatalog");
		menu8.setMainMenu(mainMenu1);
		menu8.setMenuOrder(0);
		menu8.setIconCls("icon-app");
		menu8.setLeaf(true);
		menuService.add(menu8);
		//8
		Menu mainMenu2 = new Menu();
		mainMenu2.setName("系统功能");
		mainMenu2.setMenuOrder(99);
		mainMenu2.setLeaf(false);
		menuService.add(mainMenu2);
		//9
		Menu menu6 = new Menu();
		menu6.setName("个人信息");
		menu6.setTarget("user!pl_info");
		menu6.setCode("PersonalInfo");
		menu6.setMainMenu(mainMenu2);
		menu6.setMenuOrder(1);
		menu6.setIconCls("icon-menu-user-info");
		menu6.setLeaf(true);
		menuService.add(menu6);
		//10
		Menu menu7 = new Menu();
		menu7.setName("注销系统");
		menu7.setTarget("user!logout");
		menu7.setCode("LogOut");
		menu7.setMainMenu(mainMenu2);
		menu7.setMenuOrder(99);
		menu7.setIconCls("icon-menu-logout");
		menu7.setLeaf(true);
		menuService.add(menu7);
		//11
		Menu mainMenu3 = new Menu();
		mainMenu3.setName("邮件管理");
		mainMenu3.setMenuOrder(2);
		mainMenu3.setLeaf(false);
		menuService.add(mainMenu3);
		//12
		Menu menu9 = new Menu();
		menu9.setName("写邮件");
		menu9.setTarget("mail!write");
		menu9.setCode("WriteMail");
		menu9.setMainMenu(mainMenu3);
		menu9.setMenuOrder(1);
		menu9.setIconCls("icon-app");
		menu9.setLeaf(true);
		menuService.add(menu9);
		//13
		Menu menu10 = new Menu();
		menu10.setName("收件夹");
		menu10.setTarget("mail!receive");
		menu10.setCode("Mail");
		menu10.setMainMenu(mainMenu3);
		menu10.setMenuOrder(2);
		menu10.setIconCls("icon-app");
		menu10.setLeaf(true);
		menuService.add(menu10);
		//14
		Menu menu11 = new Menu();
		menu11.setName("未读邮件");
		menu11.setTarget("mail!unRead");
		menu11.setCode("UnReadMail");
		menu11.setMainMenu(mainMenu3);
		menu11.setMenuOrder(3);
		menu11.setIconCls("icon-app");
		menu11.setLeaf(true);
		menuService.add(menu11);
		//15
		Menu menu12 = new Menu();
		menu12.setName("已发送邮件");
		menu12.setTarget("mail!send");
		menu12.setCode("SendMail");
		menu12.setMainMenu(mainMenu3);
		menu12.setMenuOrder(4);
		menu12.setIconCls("icon-app");
		menu12.setLeaf(true);
		menuService.add(menu12);
		//16
		Menu mainMenu4 = new Menu();
		mainMenu4.setName("系统公告");
		mainMenu4.setMenuOrder(1);
		mainMenu4.setLeaf(false);
		menuService.add(mainMenu4);
		//17
		Menu menu13 = new Menu();
		menu13.setName("系统公告");
		menu13.setTarget("news!show");
		menu13.setCode("News");
		menu13.setMainMenu(mainMenu4);
		menu13.setMenuOrder(4);
		menu13.setIconCls("icon-app");
		menu13.setLeaf(true);
		menuService.add(menu13);
		//18
		Menu mainMenu5 = new Menu();
		mainMenu5.setName("流程管理");
		mainMenu5.setMenuOrder(97);
		mainMenu5.setLeaf(false);
		menuService.add(mainMenu5);
		//19
		Menu menu14 = new Menu();
		menu14.setName("流程部署管理");
		menu14.setTarget("workflowDeployment!show");
		menu14.setCode("WorkflowDeployment");
		menu14.setMainMenu(mainMenu5);
		menu14.setMenuOrder(1);
		menu14.setIconCls("icon-app");
		menu14.setLeaf(true);
		menuService.add(menu14);
		//20
		Menu menu15 = new Menu();
		menu15.setName("流程挂接管理");
		menu15.setTarget("workflowProcessFunMount!show");
		menu15.setCode("WorkflowProcessFunMount");
		menu15.setMainMenu(mainMenu5);
		menu15.setMenuOrder(2);
		menu15.setIconCls("icon-app");
		menu15.setLeaf(true);
		menuService.add(menu15);
		
		/**
		 * 数据字典初始化
		 */
		Dictcatalog dictcatalog = new Dictcatalog();
		dictcatalog.setDictName("性别");
		dictcatalog.setCode("Sex");
		dictcatalog.setDes("性别信息");
		dictcatalogService.save(dictcatalog);
		
		Dictionary dictionary = new Dictionary();
		dictionary.setD_key("0");
		dictionary.setD_word("男");
		dictionary.setDictCatalog(dictcatalog);
		dictionaryService.save(dictionary);
		
		Dictionary dictionary1 = new Dictionary();
		dictionary1.setD_key("1");
		dictionary1.setD_word("女");
		dictionary1.setDictCatalog(dictcatalog);
		dictionaryService.save(dictionary1);
		
		Dictcatalog dictcatalog1 = new Dictcatalog();
		dictcatalog1.setDictName("职位状态");
		dictcatalog1.setCode("PostStatus");
		dictcatalog1.setDes("用户在职，离职状态");
		dictcatalogService.save(dictcatalog1);
		
		Dictionary dictionary2 = new Dictionary();
		dictionary2.setD_key("0");
		dictionary2.setD_word("在职");
		dictionary2.setDictCatalog(dictcatalog1);
		dictionaryService.save(dictionary2);
		
		Dictionary dictionary3 = new Dictionary();
		dictionary3.setD_key("1");
		dictionary3.setD_word("离职");
		dictionary3.setDictCatalog(dictcatalog1);
		dictionaryService.save(dictionary3);
		
		Dictcatalog dictcatalog2 = new Dictcatalog();
		dictcatalog2.setDictName("锁定状态");
		dictcatalog2.setCode("LockStatus");
		dictcatalog2.setDes("用户是否锁定状态");
		dictcatalogService.save(dictcatalog2);
		
		Dictionary dictionary4 = new Dictionary();
		dictionary4.setD_key("false");
		dictionary4.setD_word("否");
		dictionary4.setDictCatalog(dictcatalog2);
		dictionaryService.save(dictionary4);
		
		Dictionary dictionary5 = new Dictionary();
		dictionary5.setD_key("true");
		dictionary5.setD_word("是");
		dictionary5.setDictCatalog(dictcatalog2);
		dictionaryService.save(dictionary5);
		
		/**
		 * 角色初始化
		 */
		Role role1 = new Role();
		role1.setRoleName("超级管理员");
		role1.setSystemState(true);
		roleService.save(role1);
		
		Role role2 = new Role();
		role2.setRoleName("系统管理员");
		role2.setSystemState(true);
		roleService.save(role2);
		
		Role role3 = new Role();
		role3.setRoleName("基础功能");
		role3.setSystemState(true);
		roleService.save(role3);
		/**
		 * 权限初始化
		 */
		
		
		permissionService.add("1", "2", "1");
		permissionService.add("1", "3", "1");
		permissionService.add("1", "4", "1");
		permissionService.add("1", "5", "1");
		permissionService.add("1", "6", "1");
		permissionService.add("1", "7", "5,6,9,12,13");
		permissionService.add("1", "17", "5,6,9,12,14");
		
		/*authorityService.add(role1, menu1, "1");
		authorityService.add(role1, menu2, "1");
		authorityService.add(role1, menu3, "1");
		authorityService.add(role1, menu4, "1");
		authorityService.add(role1, menu5, "1");
		authorityService.add(role1, menu8, "5,6,9,12,13");
		authorityService.add(role1, menu13, "5,6,9,12,14");*/
		
		permissionService.add("1", "19", "1");
		permissionService.add("1", "20", "1");

		/*authorityService.add(role2, menu14, "1");
		authorityService.add(role2, menu15, "1");*/
		
		permissionService.add("2", "9", "1");
		permissionService.add("2", "10", "1");
		
		/*authorityService.add(role3, menu6, "1");
		authorityService.add(role3, menu7, "1");*/
		
		permissionService.add("3", "12", "1");
		permissionService.add("3", "13", "1");
		permissionService.add("3", "14", "1");
		permissionService.add("3", "15", "1");
	
		/*authorityService.add(role3, menu9, "1");
		authorityService.add(role3, menu10, "1");
		authorityService.add(role3, menu11, "1");
		authorityService.add(role3, menu12, "1");*/
		
		/**
		 * 系统管理员初始化
		 */
		Set<Role> roles = new HashSet<Role>();
		roles.add(role1);
		roles.add(role2);
		roles.add(role3);
		
		Person person = new Person();
		person.setDepart(depart1);
		person.setName("王琦");
		person.setRoles(roles);
		person.setSex("男");
		personService.save(person);
		
		T_User user = new T_User();
		user.setLoginName("wangqi");
		user.setPassword(W7.encode("19870301"));
		user.setPerson(person);
		user.setCreateDate(new Date());
		user.setStatus("在职");
		user.setLocked("否");
		userService.save(user);
		


		
		
		this.addFieldError("message", "生成系统数据完成!");
		return SUCCESS;
	}
	
	/**
	 * 生成测试数据
	 */
	public String testData(){
		//模块测试数据
	
		
		//角色测试数据
/*		RoleVo role4 = new RoleVo();
		role4.setName("测试角色1");
		roleService.add(role4);
		
		RoleVo role2 = new RoleVo();
		role2.setName("测试角色2");
		roleService.add(role2);
		
		RoleVo role3 = new RoleVo();
		role3.setName("测试角色3");
		roleService.add(role3);*/
		
		this.addFieldError("message", "生成测试数据完成!");
		return SUCCESS;
	}	
	
}
